package com.zis.base.impl;

import com.zis.base.BaseInter;
import com.zis.core.SearchParams;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Create by wangshen 2017/12/19
 */
public abstract class BaseInterImpl<T> implements BaseInter<T> {

    private Class<T> cls = null;

    private String queryName = "";

    public BaseInterImpl() {
        Class clz = this.getClass();
        ParameterizedType type = (ParameterizedType) clz.getGenericSuperclass();
        Type[] types = type.getActualTypeArguments();
        cls = (Class<T>) types[0];
        String className = cls.getSimpleName();
        className = className.substring(0,1).toLowerCase()+className.substring(1);
        queryName = className+".query";
    }

    @Autowired
    private SQLManager sqlManager;

    @Override
    public PageQuery<T> findByPage(SearchParams params) throws Exception {
        PageQuery<T> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        sqlManager.pageQuery(queryName, cls,query);
        return query;
    }
}
